<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href=".././css/login.css">
    <script >
    const frm = {
        iptUserName: null,
        iptPassword: null,
        pUserNameErr: null,
        pPasswordErr: null,
        pFrmErr: null
    };
    function onFocusOrInput(evt){
        const obj = evt.target;
        frm.pFrmErr.innerText = '';
        if (obj === frm.iptUserName){
            frm.pUserNameErr.innerText = '';
            const  userName = frm.iptUserName.value;
            console.log(userName);
            if (userName.trim().length <=0){
                frm.pUserNameErr.innerText = '用户名不能为空';
                obj.className = 'ip-error';
            }else if(userName.trim().length < 3){
                frm.pUserNameErr.innerText = '用户名长度不能小于3';
                obj.className = 'ipt-error';
            }else {
                obj.className = '';
            }
        }

    }
    window.onload = function () {
        frm.iptUserName = document.getElementById('userName');
        frm.iptUserName.addEventListener('focus', function(evt) {
          onFocusOrInput(evt);
        });
        frm.iptUserName.addEventListener('input', function(evt) {
          onFocusOrInput(evt);
        });
        frm.iptPassword = document.getElementById('password');
        frm.pUserNameErr= document.getElementById('userNameErr');
        frm.pPasswordErr= document.getElementById('passwordErr');
        frm.pFrmErr = document.getElementById('frmErr');
      }
</script>
</head>
<body>
    <div class="container">
    <form action="/login" method="post">
    <p id="frmErr" class="error"></p>
        <div class="login">
            <h2>Login</h2>
            <div class="box">
                <div class="form-group">
                <p id="userNameErr" class="error"></p>
                    <label for="userName">账号</label>
                    <input type="text" name="userName" id="userName">
                </div>
                <div class="form-group">
                <p id="passwordErr" class="error"></p>
                    <label for="password">密码</label>
                    <input type="password" name="password" id="password">
                </div>
                <div id="alert">测试文字</div>
                <button id="btn">登录</button>
            </div>
        </div>
        </form>
    </div>
</body>
</html>